<?php include("includes/session.php"); ?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $lang['PAGE_TITLE_EDIT_RESOURCES']; ?></title>
<?php include('includes/categories.php'); ?>
	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo GOOGLE_KEY; ?>" type="text/javascript">
	</script>
    <script type="text/javascript">
    function initialize()
	{
      if (GBrowserIsCompatible())
	  {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(0, 0), 1);
        map.setUIToDefault();
        map.setMapType(G_HYBRID_MAP);
		var marker = new GMarker(new GLatLng(0, 0));
		<?php
		if (isset($_GET['edit']))
		{
			$retval = $database->query("SELECT * FROM ".TBL_RESOURCES." WHERE id='".$_GET['edit']."'");
			$resource = mysql_fetch_array($retval);
			$location = $resource["latitude"].",".$resource["longitude"];
			echo "marker = new GMarker(new GLatLng($location));";
			echo "map.setCenter(new GLatLng($location), 7);";
			echo "map.addOverlay(marker);";
		}
		else if ($session->userinfo["longitude"])
		{
			$location = $session->userinfo["latitude"].",".$session->userinfo["longitude"];
			echo "marker = new GMarker(new GLatLng($location));";
			echo "map.setCenter(new GLatLng($location), 7);";
			echo "map.addOverlay(marker);";
		}
		?>

		GEvent.addListener(map,"click", function(overlay, latlng)
			{
				if (latlng)
				{
					map.removeOverlay(marker);
					//var myHtml = "The GPoint value is: " + map.fromLatLngToDivPixel(latlng) + " at zoom level " + map.getZoom();
					//map.openInfoWindow(latlng, myHtml);
					document.getElementById("LatLong").value = latlng;
					marker = new GMarker(latlng);
					map.addOverlay(marker);
				}
			});
      }
    }
    </script>
<?php include('header.php'); ?>
<?php
if(!$session->logged_in)
{
?>
	<br />
	<center>
	<table cellpadding="0" cellspacing="0" width="70%">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<center><b>You Are Not Logged In</b></center><br />
		<center>Please <a href="index.php">Log In</a> to View This Page, or <a href="register.php">Register</a> Now. </center><br />
		<center>Thank You.</center>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
<?php
}
else
{
?>
<table cellpadding="20" cellspacing="0" width="100%">
<tr><td>
	<center>
	<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<center><font size="+1"><b><?php echo $lang['PROFILE_RESOURCES']; ?></b></font></center><br />
		<?php
			if(isset($_SESSION['deleteResourceSuccess']))
			{
				if(!$_SESSION['deleteResourceSuccess'])
				{
					echo "<center><font size=\"2\" color=\"#ff0000\">Resource Deletion Failed</font></center><br />";
				}
				else
				{
					echo "<center><font size=\"2\" color=\"#00c809\">Resource Deleted Successfully</font></center><br />";
				}
				unset($_SESSION['deleteResourceSuccess']);
			}
		?>
		<?php displayOwnerResources($session->username); ?>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
	<br />
	<center>
	<table cellpadding="0" cellspacing="0">
	<tr>
		<td class="topLeft"></td><td bgcolor="#dadada"></td><td class="topRight"></td>
	</tr>
	<tr bgcolor="#dadada">
	<td></td><td>
		<?php
			if(isset($_GET['edit']))
				echo "<center><font size=\"+1\"><b>Edit Resource</b></font></center><br />";
			else
				echo "<center><font size=\"+1\"><b>Add Resource</b></font></center><br />";
			
			if(isset($_SESSION['addResourceSuccess']))
			{
				if(!$_SESSION['addResourceSuccess'])
				{
					echo "<center><font size=\"2\" color=\"#ff0000\">Errors: ".$form->num_errors."</font></center><br />";
				}
				else
				{
					echo "<center><font size=\"2\" color=\"#00c809\">Resource Added Successfully</font></center><br />";
				}
				unset($_SESSION['addResourceSuccess']);
			}
			else if(isset($_SESSION['editResourceSuccess']))
			{
				if(!$_SESSION['editResourceSuccess'])
				{
					echo "<center><font size=\"2\" color=\"#ff0000\">Errors: ".$form->num_errors."</font></center><br />";
				}
				else
				{
					echo "<center><font size=\"2\" color=\"#00c809\">Resource Edited Successfully</font></center><br />";
				}
				unset($_SESSION['editResourceSuccess']);
			}
			
			if(isset($_GET['edit']))
			{
				//already called at the map
				//$retval = $database->query("SELECT * FROM ".TBL_RESOURCES." WHERE id='".$_GET['edit']."'");
				//$resource = mysql_fetch_array($retval);
		?>
		<form action="includes/process.php" method="POST">
		<input type="hidden" name="subEditResource" value="<?php echo $_GET['edit']; ?>">
		<table>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_TYPE']; ?></b></td>
			<td>
			<select name="type" onChange="setOptions(this.options[this.selectedIndex].value);">
			<option value="Knowledge" <?php if($resource['type']=="Knowledge"){ echo "selected=\"selected\""; } ?>>Knowledge</option>
			<option value="Ability" <?php if($resource['type']=="Ability"){ echo "selected=\"selected\""; } ?>>Ability</option>
			<option value="Commodity" <?php if($resource['type']=="Commodity"){ echo "selected=\"selected\""; } ?>>Commodity</option>
			<option value="Transportation" <?php if($resource['type']=="Transportation"){ echo "selected=\"selected\""; } ?>>Transportation</option>
			<option value="Raw Materials" <?php if($resource['type']=="Raw Materials"){ echo "selected=\"selected\""; } ?>>Raw Materials</option>
			<option value="Land" <?php if($resource['type']=="Land"){ echo "selected=\"selected\""; } ?>>Land</option>
			</select><?php echo $form->error("type"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_CAT']; ?></b></td>
			<td>
			<select name="category" id="category">
			<?php 
			$result = $database->query("SELECT * FROM ".TBL_CATEGORIES." WHERE type='".$resource['type']."' ORDER BY type ASC, category ASC");
			$category = mysql_fetch_array($result);
			$num_result = mysql_num_rows($result);
			for($i=0;$i<$num_result;$i++)
			{
				$name = mysql_result($result,$i,"category");
				echo "<option value=\"$name\"";
				if($name == $resource['category'])
				{
					echo "selected=\"selected\"";
				}
				echo ">$name</option>";
			}
			?>
			</select><?php echo $form->error("category"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_NAME']; ?></b></td>
			<td>
			<input type="text" name="title" maxlength="50" value="<?php echo $resource['title'] ?>">
			</td>
			<td><?php echo $form->error("title"); ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_DESC']; ?></b></td>
			<td><textarea name="description" rows="3" cols="45"><?php echo $resource['description'] ?></textarea></td>
			<td><?php echo $form->error("description"); ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_LOCATION']; ?></b></td>
			<td><div id="map_canvas" style="width: 400px; height: 300px"></div>
				<input type="hidden" id="LatLong" name="LatLong"></td>
			<td><?php echo $form->error("location"); ?></td>
		</tr>
		<tr>
			<td align="right"></td>
			<td><input type="submit" value="Save Resource"> <input type="button" value="Cancel" onClick="buttonClick('editResources.php');"></td>
			<td></td>
		</tr>
		</table>
		</form>
		<?
			}
			else
			{
		?>
		<form action="includes/process.php" method="POST">
		<input type="hidden" name="subAddResource" value="1">
		<table>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_TYPE']; ?></b></td>
			<td>
			<select name="type" onChange="setOptions(this.options[this.selectedIndex].value);">
			<option value="">-Resource Type-</option>
			<option value="Knowledge" <?php if($form->value("type")=="Knowledge"){ echo "selected=\"selected\""; } ?>>Knowledge</option>
			<option value="Ability" <?php if($form->value("type")=="Ability"){ echo "selected=\"selected\""; } ?>>Ability</option>
			<option value="Commodity" <?php if($form->value("type")=="Commodity"){ echo "selected=\"selected\""; } ?>>Commodity</option>
			<option value="Transportation" <?php if($form->value("type")=="Transportation"){ echo "selected=\"selected\""; } ?>>Transportation</option>
			<option value="Raw Materials" <?php if($form->value("type")=="Raw Materials"){ echo "selected=\"selected\""; } ?>>Raw Materials</option>
			<option value="Land" <?php if($form->value("type")=="Land"){ echo "selected=\"selected\""; } ?>>Land</option>
			</select><?php echo $form->error("type"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_CAT']; ?></b></td>
			<td>
			<select name="category" id="category">
			<option value="">-Category Type-</option>
			</select><?php echo $form->error("category"); ?>
			</td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_NAME']; ?></b></td>
			<td>
			<input type="text" name="title" maxlength="50" value="<?php echo $form->value("title"); ?>">
			</td>
			<td><?php echo $form->error("title"); ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_DESC']; ?></b></td>
			<td><textarea name="description" rows="3" cols="45"><?php echo $form->value("description"); ?></textarea></td>
			<td><?php echo $form->error("description"); ?></td>
		</tr>
		<tr>
			<td align="right"><b><?php echo $lang['EDIT_RESOURCES_LOCATION']; ?></b></td>
			<td><div id="map_canvas" style="width: 400px; height: 300px"></div>
				<input type="hidden" id="LatLong" name="LatLong"></td>
			<td><?php echo $form->error("location"); ?></td>
		</tr>
		<tr>
			<td align="right"></td>
			<td><input type="submit" value="Add Resource"></td>
			<td></td>
		</tr>
		</table>
		</form>
		<?
			}
		?>
	</td><td></td>
	</tr>
	<tr>
		<td class="bottomLeft"></td><td bgcolor="#dadada"></td><td class="bottomRight"></td>
	</tr>
	</table>
	</center>
</td></tr>
</table>
<?php } ?>
<?php include("footer.php"); ?>